<?php
/*
  Microblog bot for game release notifications
  Copyright (C) 2017  Alexander Yakovlev

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

namespace Source;

use \Game;

class Anivisual extends Source {
  public $title = "Anivisual";
  protected $months = [
    'Января' => 'January',
    'Февраля' => 'February',
    'Марта' => 'March',
    'Апреля' => 'April',
    'Мая' => 'May',
    'Июня' => 'June',
    'Июля' => 'July',
    'Августа' => 'August',
    'Сентября' => 'September',
    'Октября' => 'October',
    'Ноября' => 'November',
    'Декабря' => 'December',
  ];
  protected function parse() {
    $text = $this->get_text('http://anivisual.net/stuff/1');
    try {
      $this->dom->loadStr($text, []);
    } catch (\Exception $e) {
      echo $e->getMessage();
      echo $e->getTraceAsString();
      return "";
    }
    unset($text);
    $games = $this->dom->find('.entryBlock');
    foreach ($games as $gameBlock) {
      $date = trim($gameBlock->find('.icon-calendar')->innerHtml);
      foreach ($this->months as $ruM => $enM) {
        $date = str_replace($ruM, $enM, $date);
      }
      $date = \DateTime::createFromFormat('d F Y', $date);
      $date = $date->format('U');
      if ($date < $this->period) continue;
      $game = new Game;
      $link = $gameBlock->find('.novel-ttl a')[0];
      $game->title = $link->innerHtml;
      $game->url = 'http://anivisual.net'.$link->getAttribute('href');
      
      $this->games[] = $game;
    }
  }
}
